#include "CmdSystem.h"
#include "CVarSystem.h"

CmdSystem cmdSystemLocal;
CmdSystem*  cmdSystem = &cmdSystemLocal;

CmdSystem::CmdSystem() {

}

CmdSystem::~CmdSystem() {

}

void CmdSystem::Init() {
    // add internal cmds ...


}

void CmdSystem::Shutdown() {

}

void CmdSystem::AddCmd( const char *cmdName, const char* description, cmdFlags_t flags, cmdFunction_t func )
{
	Cmd cmd;
	cmd.name = cmdName;
	cmd.description = description;
	cmd.function = func;
	cmd.flags = flags;

	cmds.append( cmd );
}

void CmdSystem::RemoveCmd( const char *cmd )
{

}

void CmdSystem::RemoveCmdsByFlags( int flags )
{

}

const Cmd* CmdSystem::FindCmd( const char* name ) const
{
	for( int i=0; i<cmds.size(); ++i ) {
		if( inf::Str::cmp( cmds[i].name, name ) == 0 ) {
			return &(cmds[i]);
		}
	}
	return NULL;
}

void CmdSystem::ExecuteCmd( const char *cmd )
{
    inf::CmdArgs args;
	args.TokenizeString( cmd );

	int argc = args.Argc();
	const char* cmdName = args.Argv(0);

	if( argc < 2 ) {
		const Cmd* found = FindCmd( cmdName );
		if( found )
			found->function( args );
		return;
	}

    inf::printf( "Not exist this command!\n" );
}
